package fr.axones.pocketagile.ui.controler.command.authentication
{	
	import fr.axones.pocketagile.services.remote.AuthenticationService;
	import fr.axones.pocketagile.ui.controler.Controler;
	import fr.axones.pocketagile.ui.view.authentication.AuthenticationEvent;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class AuthenticationPasswordRecoveryCommand
	{
		[Inject(id='authenticationService')]
		public var authenticationService:AuthenticationService;
		
		[Inject(id='controler')]
		public var controler:Controler;
		
		public static const SUCCESS:String = "successPasswordRecovery";
		
		public static const ERROR_NOT_FOUND:String = "notFoundPasswordRecovery";
		public static const ERROR_CONNECTION:String = "connectionPasswordRecovery";
		
		public function AuthenticationPasswordRecoveryCommand()
		{
		}
		
		[Command(selector="passwordRecovery")]
		public function execute(event:AuthenticationEvent):AsyncToken
		{						
			return authenticationService.passwordRecovery(event.email);
		}
		
		[CommandResult(selector="passwordRecovery")]
		public function result(result:ResultEvent,event:AuthenticationEvent):void
		{								
			if(result.result.status == 204)
			{
				controler.throwSuccess(SUCCESS,result.result.data,result.result.status);
			}
			else
			{
				controler.throwError(ERROR_NOT_FOUND,null,1);
			}
		}
		
		[CommandError(selector="passwordRecovery")]
		public function fault(fault:FaultEvent,event:AuthenticationEvent):void
		{
			controler.throwError(ERROR_CONNECTION,null,0);
		}
	}
}